import { Injectable } from '@angular/core';
import { Events } from 'ionic-angular';
import { DialogProvider } from '../../providers/dialog/dialog';
/*
  Generated class for the EventProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class EventProvider {

	public EVENT_LOGIN = "eventLogin";
	public EVENT_PROGRESS_VALUE = 'eventprogressvalue';
	public EVENT_Timeselect = "eventTime-select";
	public EVENT_BLEconnect = "eventBLE-connect";
	public EVENT_BLEdevice = "eventBLE-device";

	constructor(public events: Events,
		public dialog: DialogProvider,) {
		console.log('Hello EventProvider Provider');
	}

	/**
		 * 注册事件 the event handler（订阅消息）
		 */
	public bindEvents(name: string, handler: any) {
		let a = this.events.unsubscribe(name);//先解除
		// this.dialog.showAlert(name, JSON.stringify(a));
		this.events.subscribe(name, handler);//再注册
		// this.dialog.showAlert('注册'+name, '');
	}

	/**
	 * 响应事件 （取消订阅）
	 */
	public unbindEvents(name: string, handler: any) {
		let a = this.events.unsubscribe(name);
		// this.dialog.showAlert(name, JSON.stringify(a));
	}

	/**
	 * 响应事件 （发布订阅）
	 */
	public sendEvents(name: string, data: any) {
		this.events.publish(name, data);
	}

}
